﻿Imports System.Data.OleDb
Public Class ViewSandCastle
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.Params("ID") = "" Then
            Response.Redirect("Default.aspx")
        End If

        'Check if user is the creator of the event to enable or disable delete button
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim SqlStringCheckUser As String = "SELECT * FROM SandCastle WHERE Tourist = @f1 AND ID = @f2"
        Dim cmdCheck As OleDbCommand = New OleDbCommand(SqlStringCheckUser, oleDbConn)
        cmdCheck.CommandType = CommandType.Text
        cmdCheck.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmdCheck.Parameters.AddWithValue("@f2", Request.Params("ID"))

        'Getting records (only 1 line for selected tourist)
        Dim dr As OleDbDataReader = cmdCheck.ExecuteReader()
        If dr.HasRows Then
            dr.Read()
            If Not IsDBNull(dr("ID")) Then
                btn_DelSandCastle.Visible = True
                btn_DelSandCastle.Enabled = True
            End If
        End If

        If IsPostBack = False Then
            'Count Activity
            Dim SqlString As String = "INSERT INTO SiteActivity(Activity) VALUES (@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", "SandCastles")
            cmd.ExecuteNonQuery()

            'Set the heading of page
            Dim SqlStringCastleDetails As String = "SELECT * FROM SandCastle WHERE ID = @f1"
            Dim cmdCastleDetails As OleDbCommand = New OleDbCommand(SqlStringCastleDetails, oleDbConn)
            cmdCastleDetails.CommandType = CommandType.Text
            cmdCastleDetails.Parameters.AddWithValue("@f1", Request.Params("ID"))

            Dim drCastleDetails As OleDbDataReader = cmdCastleDetails.ExecuteReader()
            drCastleDetails.Read()
            If drCastleDetails.HasRows Then
                myHeading.InnerHtml = drCastleDetails("CastleName") & "<br/>by " & drCastleDetails("Tourist")
            End If

            'Check if user has voted this album
            Dim hasVotedsql As String = "SELECT * FROM SandCastle_Votes WHERE SandCastle_FK = @f1 AND Voter = @f2"
            Dim hasVotedcmd As OleDbCommand = New OleDbCommand(hasVotedsql, oleDbConn)
            hasVotedcmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            hasVotedcmd.Parameters.AddWithValue("@f2", User.Identity.Name)

            'Find out if there any records at all (returned)
            Dim drVotes As OleDbDataReader = hasVotedcmd.ExecuteReader()
            'this shows if any data is returned
            If drVotes.HasRows Then
                btn_VoteCastle.Enabled = False
                btn_VoteCastle.Text = "Voted"
            Else
                btn_VoteCastle.Enabled = True
                btn_VoteCastle.Text = "Vote"
            End If
            oleDbConn.Close()
        End If
    End Sub

    Protected Sub btn_VoteCastle_Click(sender As Object, e As EventArgs) Handles btn_VoteCastle.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim addVotesql As String = "INSERT INTO SandCastle_Votes (SandCastle_FK, Voter) VALUES (@f1, @f2)"
        Dim addVotecmd As OleDbCommand = New OleDbCommand(addVotesql, oleDbConn)
        addVotecmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        addVotecmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        addVotecmd.ExecuteNonQuery()
        oleDbConn.Close()
        oleDbConn.Dispose()
        addVotecmd.Dispose()
        'Update the repeater to display the vote
        rpt_Votes.DataBind()
        'hide button
        btn_VoteCastle.Enabled = False
        btn_VoteCastle.Text = "Voted"
    End Sub

    Protected Sub btn_DelSandCastle_Click(sender As Object, e As EventArgs) Handles btn_DelSandCastle.Click
        'First remove people who have voted the sandcastle
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim SqlString As String = "DELETE FROM SandCastle_Votes WHERE SandCastle_FK = @f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.ExecuteNonQuery()

        'Deleting the event.
        Dim SqlStringEvent As String = "DELETE FROM SandCastle WHERE ID = @f1"
        Dim cmdEvent As OleDbCommand = New OleDbCommand(SqlStringEvent, oleDbConn)
        cmdEvent.CommandType = CommandType.Text
        cmdEvent.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmdEvent.ExecuteNonQuery()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("SandCastle.aspx")
    End Sub
End Class